<template>
    <div>
                <form action="/">
  <van-search  
    v-model="value"
    show-action
    placeholder="请输入搜索关键词"
    @search="onSearch"
    @cancel="onCancel"
    @keyup.enter="onSearch"
  >
  <template #label>
       <van-icon name="arrow-left" @click="$router.go(-1)" />
  </template>
  <template #action>
    <div @click="onSearch" v-if="value">搜索</div>
    <div @click="$router.go(-1)" v-if="!value">取消</div>
  </template>
  </van-search>
</form>
<h5 v-if='show'>搜索历史 <van-icon name="delete-o" @click="all"/></h5>
 <div class="text" v-if='show'>
    <van-button type="default" style="margin:.2rem" v-for="(item,index) in $store.state.searchList" :key="index" @click="onSearch(item.title,'btn')">{{item.title}}</van-button>
</div>
    </div>
</template>

<script>
import {courseLists} from "../../Request/http.js";
export default {
    name: 'EverydaySearch',

    data() {
        return {
             value: "",
      list:[],
      show:true,
      show1:2,
      show2:false
        };
    },

    mounted() {
        
    },

    methods: {
          all(){
      this.$store.commit('all')
    },
    onSearch(val,btn) {
        // this.$store.state.searchList=Array.from(new Set(this.$store.state.searchList))
       
       if(btn=='btn'){
             courseLists({keywords:val}).then(res=>{
        this.list=res.list
        this.show1=false
        if(this.list.length!=0){
          this.show=true
           this.show2=true
        }else{
          this.show=false
          this.show2=false
        }
        console.log(this.list);
        this.$router.push('/paperPage')
      })
       }else{
          var index = this.$store.state.searchList.findIndex(item => {
          return (item.title == this.value);
        });
        console.log(index);
       if(index!=-1){
         this.$store.commit('detail',index)
       }
       this.$store.commit("add", { title: this.value });
       console.log(val,btn);
             courseLists({keywords:this.value}).then(res=>{
        this.list=res.list
       
      })
       }
     
      if (this.$store.state.searchList.length > 6) {
        this.$store.commit("pop");
      } 
       this.$router.push('/paperPage')
      this.value=''
    },
     onCancel() {
      this.$toast("取消");
    }
    },
};
</script>

<style lang="scss" scoped>

</style>